/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.handler;

import java.util.Collection;
import java.util.HashSet;

import net.cellobject.javacodeformatter.model.IFormatterItem;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.part.ResourceTransfer;

/**
 * Copy each currently selected object in the Favorites view to the clipboard.
 */
public class CopyFormatterHandler extends ClipboardHandler {
	/**
	 * Called by the superclass {@link #execute(ExecutionEvent)} method so that this method can concentrate on the operation and
	 * does not have to manage clipboard creation and disposal.
	 */
	@Override
	public Object execute(final ExecutionEvent event, final Clipboard clipboard) throws ExecutionException {
		final ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			final Object[] objects = ((IStructuredSelection)selection).toArray();
			if (objects.length > 0) {
				try {
					clipboard.setContents(new Object[]{asResources(objects), asText(objects),},
							new Transfer[]{ResourceTransfer.getInstance(), TextTransfer.getInstance(),});
				} catch (final SWTError error) {
					// Copy to clipboard failed.
					// This happens when another application
					// is accessing the clipboard while we copy.
					// Ignore the error.
				}
			}
		}
		return null;
	}

	public static IResource[] asResources(final Object[] objects) {
		final Collection<IResource> resources = new HashSet<IResource>(objects.length);
		for (final Object each : objects) {
			if (each instanceof IAdaptable) {
				final IResource res = (IResource) ((IAdaptable)each).getAdapter(IResource.class);
				if (res != null) {
					resources.add(res);
				}
			}
		}
		return resources.toArray(new IResource[resources.size()]);
	}

	public static String asText(final Object[] objects) {
		final StringBuffer buf = new StringBuffer();
		for (final Object each : objects) {
			if (each instanceof IFormatterItem) {
				buf.append("Favorite: ");
				buf.append( ((IFormatterItem)each).getName());
			} else if (each != null) {
				buf.append(each.toString());
			}
			buf.append(System.getProperty("line.separator"));
		}
		return buf.toString();
	}
}
